package com.byit.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.domain.AppCharacteristic;
import com.byit.domain.AppUser;
import com.byit.enums.PromptInfo;
import com.byit.jwt.ann.JwtUser;
import com.byit.jwt.domain.JwtSession;
import com.byit.msg.MessageResult;
import com.byit.service.*;
import com.byit.utils.IpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP首页数据获取
 *
 * @author tjm
 * @date 2019年1月2日
 */
@RestController
@RequestMapping("/app/home")
public class AppHomeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAppBannerService appBannerService;
    @Autowired
    private IAppMessageNoticeService appMessageNoticeService;
    @Autowired
    private IAppApplicationService appApplicationService;
    @Autowired
    private IAppUserApplictionService appUserApplictionService;
    @Autowired
    private IAppUserService appUserService;
    @Autowired
    private IAppCharacteristicService appCharacteristicService;


    /**
     * app首页获取统计数据，banner数据，通知公告数据和应用数据
     */
    @GetMapping("/bannerAndNotice")
    public Object bannerAndNotice(@JwtUser JwtSession session, HttpServletRequest request) {
        String ipAddr = IpUtils.getRequestURL(request);
        Map<String, Object> data = new HashMap<>();
        //banner
        List<JSONObject> list = appBannerService.selectAppHomeBanner();
        //通知公告
        Map<String, Object> params = MapBuilder.newMap(4)
                .setParam("category", 1)
               .ok();
        Page<JSONObject> messageNoticeAll = appMessageNoticeService.selectAppMessageNoticeAll(new Page<>(1, 5), params);
        //app应用
        Page<JSONObject> apps = null;
        if (session.isLogin()) {
            Long userId = session.userId();
            apps = appUserApplictionService.selectDataPage(new Page<>(1, 7), MapBuilder.newMap(1).setParam("userId", userId).ok());
        }
        if (apps == null || apps.getRecords().size() == 0) {
            apps = appApplicationService.selectDataPage(new Page<>(1, 7), null);
        }
        List<JSONObject> records = apps.getRecords();
        for (JSONObject obj : records) {
            String url = obj.getString("url");
            if (null == url || (url.indexOf("https://") < 0 && url.indexOf("http://") < 0 && url.indexOf("app") < 0)) {
                obj.put("url", ipAddr + "/app/h5/noData.html");
            }
        }
        //特色江油
        EntityWrapper<AppCharacteristic> wrapperTs = new EntityWrapper<>();
        wrapperTs.setSqlSelect("id,content,url");
        wrapperTs.eq("type", "P_O_0");
        wrapperTs.eq("is_del", 0);
        List<Map<String, Object>> mapList = appCharacteristicService.selectMaps(wrapperTs);
        //政民互动
        EntityWrapper<AppCharacteristic> wrapperZm = new EntityWrapper<>();
        wrapperZm.setSqlSelect("id,content,url");
        wrapperZm.eq("type", "T_O_0");
        wrapperZm.eq("is_del", 0);
        List<Map<String, Object>> zmhdList = appCharacteristicService.selectMaps(wrapperZm);
        //功能统计
        Map<String, Object> map = appApplicationService.selectDataCount();
        //用户数统计
        EntityWrapper<AppUser> wrapper = new EntityWrapper<>();
        wrapper.eq("is_del", 0);
        map.put("userCount", appUserService.selectCount(wrapper));
        //TODO 解决事项 还未统计
        map.put("matterCounr", 0);
        data.put("banner", list);
        data.put("apps", records);
        data.put("counts", map);
        data.put("notice", messageNoticeAll.getRecords());
        data.put("characteristic", mapList.size() > 0 ? mapList.get(0) : null);
        data.put("zmhd", zmhdList.size() > 0 ? zmhdList.get(0) : null);
        return MessageResult.warpper(PromptInfo.SYS_SECCUSS, data);
    }


}
